﻿/// <reference path="../../../Js/jquery-1.11.1.min.js" />
/// <reference path="jquery.dataTables.min.js" />
/// <reference path="../../../../Controls/Common/duomi.common.js" />

/*************************************************************
DataTables Custom Search.
JQ DataTables 自定义搜索 扩展.
Create By Ming.ma
2014-09-25 V1.0.1 获取字段名 使用sName
2014-08-20 V1.0


利用 DataTables API 方法 fnServerParams 实现.
参考1: http://legacy.datatables.net/ref#fnServerParams

示例: 
 "fnServerParams": function (aoData) {
    aoData.push({
        "name": "sCustomFilter",
        "value": $('#div_list').dataTable().customSearch([
            { 'index': 3, 'type': 'between', 'value': $$$('txtStart').value, 'value2': $$$('txtEnd').value },
            { 'index': 4, 'type': 'equal', 'value': $$$('selStatus').value, 'value2': '' },
            { 'index': 5, 'type': 'equal', 'value': $$$('selChannel').value, 'value2': '' }
        ])
    });

    }
*************************************************************/

(function ($) {
    "use strict";
    $.fn.customSearch = function (arr) {
        var oTable = $(this).dataTable();
        var oSettings = oTable.fnSettings();
        var compare = {
            like: " like '%{0}%' and ",
            equal: " = '{0}' and ",
            between: " between '{0}' and '{1}'  and ",
            custom: '{0}'
        };
        var defaultOption = [{ 'index': 0, 'type': 'like', 'value': '', 'value2': '' }];
        var result = '';
        for (var i = 0; i < arr.length; i++) {
            if (!$string.isEmpty(arr[i].value)) {
                result += oSettings.aoColumns[arr[i].index].sName + $string.format(compare[arr[i].type], arr[i].value, arr[i].value2);
            }
        }
        return result.replace(/and\s$/ig,'');
    };
})(jQuery);